using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure.StorageClient;

namespace AzureContrib.WindowsAzure.StorageClient
{
	internal class ResultSegmentWrapper<T, TContract> : IResultSegment<TContract>
	{
		private readonly ResultSegment<T> resultSegment;
		private readonly Converter<T, TContract> converter;

		public ResultSegmentWrapper(ResultSegment<T> resultSegment, Converter<T, TContract> converter)
		{
			this.resultSegment = resultSegment;
			this.converter = converter;
		}

		public IAsyncResult BeginGetNext(AsyncCallback callback, object state)
		{
			return resultSegment.BeginGetNext(callback, state);
		}

		public IResultSegment<TContract> EndGetNext(IAsyncResult asyncResult)
		{
			var endGetNext = resultSegment.EndGetNext(asyncResult);
			return new ResultSegmentWrapper<T, TContract>(endGetNext, converter);
		}

		public IResultSegment<TContract> GetNext()
		{
			var getNext = resultSegment.GetNext();
			return new ResultSegmentWrapper<T, TContract>(getNext, converter);
		}

		public ResultContinuation ContinuationToken
		{
			get
			{
				return resultSegment.ContinuationToken;
			}
		}

		public bool HasMoreResults
		{
			get
			{
				return resultSegment.HasMoreResults;
			}
		}

		public IEnumerable<TContract> Results
		{
			get
			{
				return resultSegment.Results.Select(rs => converter(rs));
			}
		}
	}
}